/**
 * related to circleDetailCell1.ui
 * 用于加载：圈子详情-上部份数据
 * @Author : yaohuan
 * @Timestamp : 2016-07-01
 */

//引入组件库
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var circleUrl = require("url/circle");
var http = require("helper/http");
var drawUrl = require("url/draw");
var common = require("common");
var userUtil = require("helper/user");
var imHelper = require("helper/im");
var open = require("helper/open");
var indexCacher = require("helper/indexCacher");

//声明UI变量
var root=ui("$");  //$表示当前视图的根UI
var joinCircleHttpUrl = circleUrl.url.v1_4.joinCircle;
var membersHttpUrl = circleUrl.url.member.members4circle;
var signCircleHttpUrl = circleUrl.url.sign.sign;

var imgGridMember = ui("imgGridMember");

//设置数据绑定的映射关系
root.setMapping({
	"do_Button_join.tag":"id",
	"lableCircleName.text":"name",
	"lableCircleName.tag":"isLottery",
	"lableCircleCreator.tag":"creatorId",
	"lableCircleCreator.text":"creatorName",
	"imglogo.source":"fullLogo",
	"lableCircleType.visible":"isOfficial",
	"do_ALayout_join.visible":"canJion",
	"do_ALayout_chat.visible":"canChat",
	"do_ALayout_sign.enabled":"canSign",
	"do_ALayout_sign.visible" : "isShowSign",
	"lableRemark.text":"remark",
	"lableRemark.tag":"funcType",
	"do_ALayout_member.visible":"hasImg",//是否有成员图片
	"imgGridMember.items":"imgItems" //成员图片集合
});

root.on("dataRefreshed", function(){
	var canSign = ui("do_ALayout_sign").enabled;//是否可以签到
	setSignBtnStatus(canSign);
});

/**
 * @param vsCanSign 是否可以点击签到
 * @returns
 */
var signBtn = ui("btnSign");
function setSignBtnStatus(vsCanSign){
	if(vsCanSign){
		signBtn.border ="FF7E00FF,1,[0,0,0,0]";//黄色边框
		signBtn.fontColor = "FF7E00FF";//黄字
		signBtn.bgColor = "FFFFFFFF";//白底
		signBtn.text = "签到";
	}else{
		signBtn.border ="";//没有边框
		signBtn.fontColor = "FFFFFFFF";//白字
		signBtn.bgColor = "C0C0C0FF";//灰底
		signBtn.text = "已签到";
		ui("do_ALayout_sign").enabled = false;//不能点击
	}
}
//点击成员头像列表gridView,打开圈子成员页面
ui("do_ALayout_member").on("touch",function(data, e) {
	var circleId = ui("do_Button_join").tag;
	common.open("source://view/circle/member/circleMembers.ui",{circleId:circleId});
})
//加入图标--点击区域
var do_Button_join = ui("do_Button_join");
ui("do_ALayout_join").on("touch", "", 3000, function(data, e) {
	if(userUtil.loginToken()===""){
		nf.toast("您还没有登录哦.");
	}else{
		var circleId = do_Button_join.tag;
		http.post(joinCircleHttpUrl,{"circleId":circleId,"cityId":indexCacher.currentCityId()},function(data){
			if(data.myMsg){
				nf.toast(data.myMsg)
			}else{
				do_Button_join.hide();
				nf.toast("加入成功");
				//funcType: 1-全部(聊天和发话题);2-只能聊天;3-只能发话题'
				if(ui("lableRemark").tag!=3){
					ui("do_ALayout_chat").visible = true;
				}
				page.fire("join_circle_success");
			}
		});
	}
});

//群聊
ui("do_ALayout_chat").on("touch", "", 3000, function(data, e) {
	var circleId = do_Button_join.tag;
	imHelper.toGroupChat(circleId);
});


//圈主
ui("alayoutOwner").on("touch", function(data, e) {
	var creatorId = ui("lableCircleCreator").tag;
	var isOfficial = ui("lableCircleType").visible;
	if(creatorId!=null && !isOfficial){
		//非官方圈子，点击圈主头象-去他的主页
		open.openPersonal(creatorId);
	}
	
})
//点击圈子名称
ui("lineLayoutCircle").on("touch", function(data, e) {
	var circleId = ui("do_Button_join").tag;
	common.open("source://view/circle/base/detail/circleInfo.ui",{circleId:circleId});
})
//点击圈子logo
ui("do_ALayout_logo").on("touch", function(data, e) {
	var circleId = ui("do_Button_join").tag;
	common.open("source://view/circle/base/detail/circleInfo.ui",{circleId:circleId});
})

//签到--点击区域
ui("do_ALayout_sign").on("touch", function(data, e) {
	if(userUtil.loginToken()===""){
		nf.toast("您还没有登录哦.");
	}else{
		var circleId = ui("do_Button_join").tag;
		http.post(signCircleHttpUrl,{"circleId":circleId},function(data){
			nf.toast("签到成功");
			setSignBtnStatus(false);//已签到
			page.fire("circle_sign_success", circleId);//刷新列表中的圈子信息【改变签到按钮状态】
			if(ui("lableCircleName").tag==1){
				loadLottery();
			}
		});
	}
});
function loadLottery(){
	var circleId = ui("do_Button_join").tag;
	http.post(drawUrl.url.base.detail, {"circleId":circleId, "type":2}, function(data) {
		if(null!=data.drawBaseVo){
			var drawId=data.drawBaseVo.id;
			var rewardNames=[];
			var imgs=[];
			for (var i = 0; i < data.items.length; i++) {
				var item=data.items[i];
				rewardNames.push(item.drawItemName);
				imgs.push(item.drawItemImg);
				
			}
			common.open("source://view/lottery/index.ui",{userDrawList:data.userDrawList,type:2,bizzId:ui("do_Button_join").tag,drawId:drawId,rewardNames:rewardNames,imgs:imgs});
		}
	});
}
